#ifndef __CORE_FILESYS_DIRS_H__
#define __CORE_FILESYS_DIRS_H__

/**
 *  core/filesys/dirs.h
 *  funkce pro praci se slozkami
 */

/* find_file() */
#define END_OF_DIR          (-1)

/* create_dir() */
#define CREATE_DIR_OK       (1)
#define CREATE_DIR_FAILED   (0)

/* change_dir() */
#define CHDIR_OK            (0)
#define CHDIR_NOT_DIR       (1)
#define CHDIR_NOT_FOUND     (2)

/* struktura casu u souboru */
typedef struct {
	unsigned int
		sec          :5,
		min          :6,
		hour         :5;
} FileTime;

/* struktura data u souboru */
typedef struct {
  unsigned int
    day          :5,    /* 5 je pocet bitu kolikami se vyjadruje pocet dni v mesici 2^5 = 32-1 = 31dni */
    month        :4,    /* 2^4-1=15 max 15 mesicu, nam staci jen 12 :) */
    year         :7;    /* rok - 1980 */
} FileDate;

/* struktura celeho souboru ve FAT */
typedef struct {
  unsigned char   name[8];          /* jmeno */
  unsigned char   ext[3];           /* pripona */
  unsigned char   attr;             /* atribut */
  unsigned char   reserved[0x0A];   /* ... */
  FileTime        time;             /* cas */
  FileDate        date;             /* datum */
  unsigned int    ClstrNo;          /* pocatecni cluster */
  unsigned long   filesize;         /* velikost souboru */
  unsigned char   realname[13];     /* skutecne jmeno + pripona + '\0' */
} DirectoryEntry;

struct ffblk	{
  char far *    path;
  unsigned int  attr;
  int           ff_reserved;
  unsigned char ff_attrib;
  unsigned int  ff_ftime;
  unsigned int  ff_fdate;
  unsigned int  ff_fsize;
  unsigned char ff_name[13];
};

typedef struct {
  unsigned int c_start;          /* zacatek slozky   */
  unsigned int c_actual;         /* aktualni cluster */
  unsigned int pos;              /* pozice v klastru */

  /* pouze ukazatel na retezec, obsahujici cestu k aktualni slozce */
  char far *   string;           /* textovy retezec  */
} ActualDir;

typedef struct {
  /* globalni promenna funkce find_file();
     udrzuje v pameti posledni klastr,
     ktery se cetl - nemusi se tedy
     kvuli vypisu jedne slozky
     stale dokola nacitat struktura
     slozky do bufferu */
  unsigned int      last_cluster;

  /* v init_dirs() > alokujeme 512 bajtu pro sektor */
  char far *        buffer;

  /* zaloha pointeru na <_dir_find_file_buffer>, pro znovuasociovani */
  char far *        buffer_backup_pointer;
} DirFindFile;

/* struktura aktualni slozky */
ActualDir   far *   _dir_actual;

/* struktura pro find_file() */
DirFindFile far *   _dir_find_file;

int           init_dirs               (void);
int           create_dir              (char far *filename);
int           find_file_care          (DirectoryEntry far *sfile, int pos, int far *_page, int far *_pos, bool breakAtDelete);
int           find_file               (DirectoryEntry far *sfile, int pos);
int           goto_dir                (char far *nextdir);
int           change_dir              (const char far *path);
char far *    get_current_working_dir (char far *dir);

int           findfirst               (const char far *path, struct ffblk far *ffblk, int attrib);
int           findnext                (struct ffblk far *ffblk);
void          findclose               (struct ffblk far *ffblk);


#include <core/init.h>
#include <core/filesys/entries.h>
#include <source/core/filesys/dirs.c>

#endif
